#!/usr/bin/env node

const { select } = require('@inquirer/prompts');
const chalk = require('chalk');
const SyncModel = require('./sync-model');
const AutoGenerateCrud = require('./auto-generate-crud');

const Options = {
    SYNC_MODELS_FROM_DB: 'SYNC_MODELS_FROM_DB',
    AUTO_GENERATE_CRUD_FOR_MODEL: 'AUTO_GENERATE_CRUD_FOR_MODEL'
};

async function main() {
    console.log(chalk.blue('🤖 OKX Trading Robot - Express Sequelize Generator'));
    console.log(chalk.gray('================================================\n'));

    const choices = [
        {
            name: '🔄 从数据库同步模型 (Sync Models from Database)',
            value: Options.SYNC_MODELS_FROM_DB,
            description: '从现有数据库表结构生成 Sequelize 模型文件'
        },
        {
            name: '⚡ 自动生成 CRUD 代码 (Auto Generate CRUD)',
            value: Options.AUTO_GENERATE_CRUD_FOR_MODEL,
            description: '为模型自动生成控制器、路由、验证等代码'
        }
    ];

    try {
        const answer = await select({
            choices: choices,
            message: '请选择要执行的操作:',
            pageSize: 10
        });

        console.log(chalk.yellow(`\n执行操作: ${answer}\n`));

        switch (answer) {
            case Options.SYNC_MODELS_FROM_DB:
                await SyncModel.run();
                break;
            case Options.AUTO_GENERATE_CRUD_FOR_MODEL:
                await AutoGenerateCrud.run();
                break;
            default:
                console.log(chalk.red('❌ 未知选项!'));
                process.exit(1);
        }

        console.log(chalk.green('\n✅ 操作完成!'));
    } catch (error) {
        console.error(chalk.red('❌ 执行失败:'), error.message);
        process.exit(1);
    }
}

// 如果直接运行此文件
if (require.main === module) {
    main();
}

module.exports = { main, Options };
